package cc.wsyw126.java.schedule;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by junpeng.yang on 16/11/8.
 */
public class UseThreadPoolFactory {
    public static void main(String[] args) throws InterruptedException {
        int n = 100;
        ExecutorService executor = ThreadPoolFactory.newCachedThreadPool();
        for (int i = 0; i < n; i++) {
            executor.execute(new running(new ArrayList<Integer>(), i));
        }
        executor.shutdown();

        while (true) {
            boolean terminated = executor.isTerminated();
            if (terminated) {
                break;
            }
        }

        System.out.println("executor.isShutdown() = " + executor.isTerminated());


        ExecutorService executorService = ThreadPoolFactory.newFixedThreadPool();
        for (int i = 0; i < n; i++) {
            executorService.execute(new running(new ArrayList<Integer>(), i));
        }
        executorService.shutdown();


        while (true) {
            boolean terminated = executorService.isTerminated();
            if (terminated) {
                break;
            }
        }

        System.out.println("executorService.isTerminated() = " + executorService.isTerminated());


        TimeUnit.SECONDS.sleep(10);
    }
}

class running implements Runnable {
    private List<Integer> parameter = new ArrayList<>();
    private Integer i;

    public running(List<Integer> parameter, int i) {
        this.parameter = parameter;
        this.i = i;
    }

    @Override
    public void run() {
        System.out.println("i start = " + i);
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("i end = " + i);
    }
}
